package io.renren.modules.device.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.Set;

import io.renren.modules.device.entity.BuildingEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import io.renren.modules.device.entity.DeviceEntity;
import io.renren.modules.device.service.BuildingService;
import io.renren.modules.device.service.DeviceService;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 设备表
 *
 * @author lwm
 * @email 
 * @date 2019-01-10 11:49:19
 */
@RestController
@RequestMapping("device/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private BuildingService buildingService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("device:device:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = deviceService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("device:device:info")
    public R info(@PathVariable("id") Long id){
			DeviceEntity device = deviceService.selectById(id);
			BuildingEntity buildingEntity = buildingService.selectOne(new EntityWrapper<BuildingEntity>().eq("building_code",device.getBuildingCode()));
            device.setBuildingName(buildingEntity.getBuildingName());

        return R.ok().put("device", device).put("building",buildingEntity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("device:device:save")
    public R save(@RequestBody DeviceEntity device){
    	verifyForm(device);
    	device.setCreateTime(new Date());
    	String code = device.getBuildingCode().split("-")[0];
    	device.setBuildingCode(code);
		deviceService.insert(device);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("device:device:update")
    public R update(@RequestBody DeviceEntity device){
    	if(!StringUtils.hasLength(device.getDeviceId())) {
			throw new RRException("设备ID不能为空");
		}
		if(!StringUtils.hasLength(device.getImei())){
			throw new RRException("IMEI不能为空");
		}
		if(!StringUtils.hasLength(device.getBuildingCode())){
			throw new RRException("大楼编号不能为空");
		}
		deviceService.updateById(device);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("device:device:delete")
    public R delete(@RequestBody Long[] ids){
			deviceService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    
    @ResponseBody
    @RequestMapping("/getBuildingArea")
    public R getBuildingArea(int level, String parentKey) {
        Set<String> data = buildingService.getBuildingCodeArea(level, parentKey);
        return R.ok().put("data", data);
    }

    /**
	 * 验证参数是否正确
	 */
	private void verifyForm(DeviceEntity device){
		if(!StringUtils.hasLength(device.getDeviceId())) {
			throw new RRException("设备ID不能为空");
		}
		int value = deviceService.selectCount(new EntityWrapper<DeviceEntity>().eq("DEVICE_ID", device.getDeviceId()));
		if(value > 0) {
			throw new RRException("该设备ID已经存在");
		}
		if(!StringUtils.hasLength(device.getImei())){
			throw new RRException("IMEI不能为空");
		}
		int value2 = deviceService.selectCount(new EntityWrapper<DeviceEntity>().eq("IMEI", device.getImei()));
		if(value2 > 0) {
			throw new RRException("该IMEI已经存在");
		}
		if(!StringUtils.hasLength(device.getBuildingCode())){
			throw new RRException("大楼编号不能为空");
		}
	}
}
